<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="题目名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入题目名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属课程" prop="courseId">
        <el-input
          v-model="queryParams.courseId"
          placeholder="请输入所属课程"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属学科" prop="subjectId">
        <el-input
          v-model="queryParams.subjectId"
          placeholder="请输入所属学科"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="难易程度" prop="difficulty">
        <el-select
          v-model="queryParams.difficulty"
          placeholder="请选择难易程度"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in dict.type.difficulty"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="题目类型" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择题目类型"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in dict.type.type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="标签" prop="tags">
        <el-input
          v-model="queryParams.tags"
          placeholder="请输入标签"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="questionList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="题目名称" align="center" prop="name" />
      <el-table-column label="所属课程" align="center" prop="courseId" />
      <el-table-column label="所属学科" align="center" prop="subjectId" />
      <el-table-column label="难易程度" align="center" prop="difficulty">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.difficulty"
            :value="scope.row.difficulty"
          />
        </template>
      </el-table-column>
      <el-table-column label="题目类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.type" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="标签" align="center" prop="tags" />
      <el-table-column
        label="操作"
        align="center"
        width="150"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="
              $router.push({
                path: '/solve',
                query: {
                  id: scope.row.id,
                },
              })
            "
          >
            做题
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listQuestions } from "@/api/system/questions";

export default {
  name: "Questions",
  dicts: ["answer_certainty", "difficulty", "type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 问题表格数据
      questionList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        courseId: undefined,
        subjectId: undefined,
        difficulty: undefined,
        type: undefined,
        tags: undefined,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询问题列表 */
    getList() {
      this.loading = true;
      listQuestions(this.queryParams).then((response) => {
        this.questionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        courseId: undefined,
        subjectId: undefined,
        difficulty: undefined,
        type: undefined,
        stem: undefined,
        standardAnswer: undefined,
        answerCertainty: undefined,
        tags: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push("/questions/add");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push("/questions/edit/" + row.id);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const questionIds = row.id || this.ids;
      this.$confirm("是否确认删除选择的问题?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delQuestions(questionIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },
  },
};
</script>